package com.chain.qs.modules.biz.web;


import com.chain.qs.common.config.Global;
import com.chain.qs.common.persistence.Page;
import com.chain.qs.common.schedule.QuartzJobBean;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.modules.biz.entity.*;
import com.chain.qs.modules.biz.entity.EquSwitcher;
import com.chain.qs.modules.biz.service.*;
import com.chain.qs.modules.sys.entity.Office;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;


/**
 * 设备记录Controller
 *
 * @author fgx
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/switcher")
public class EquSwitcherController extends SeniorController {

    @Resource
    private StationService stationService;

    @Resource
    private EquSwitcherService equSwitcherService;

    @Resource
    private StationGroupCfgExtendService sgcfgService;

    @ModelAttribute
    public EquSwitcher get(@RequestParam(required = false) String id, Model model) {
        EquSwitcher equSwitcher;
        if (StringUtils.isNotBlank(id)) {
            equSwitcher = equSwitcherService.get(id);
            equSwitcher.setTurnWeekArray(equSwitcher.getTurnWeek().split(","));
            model.addAttribute("itemList", sgcfgService.equlistByGroup(equSwitcher.getSgroupId()));
        } else {
            equSwitcher = new EquSwitcher();
        }
        return equSwitcher;
    }

    @ModelAttribute("condition")
    public Condition getCondition(String sgroupId, String gitemId) {
        Condition condition = new Condition();
        if (sgroupId != null) {
            condition.setSgroupId(sgroupId);
        }
        if (gitemId != null) {
            condition.setGitemId(gitemId);
        }
        return condition;
    }

    @RequestMapping(value = "")
    public String sgroup(Model model) {
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        return "modules/biz/equItemSwitcherList";
    }

    //
    @RequestMapping(value = "switchPage")
    public String switcherPage(Condition condition, HttpServletRequest request, HttpServletResponse response) {//查询提交功能
        Page<EquSwitcher> page = equSwitcherService.findPage(new Page<EquSwitcher>(request, response), new EquSwitcher(), condition);
        return renderString(response, page);
    }

    @RequestMapping(value = "form")
    public String form(Model model,String id) {
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        if(id!=null && !id.equals("")){
            return "modules/biz/equItemSwitcherModify";
        }else{
            return "modules/biz/equItemSwitcherForm";
        }
    }

    @RequestMapping(value = "delete")
    public String delete(String officeId,EquSwitcher equSwitcher, RedirectAttributes redirectAttributes) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/biz/switcher";
        }
        equSwitcherService.delete(equSwitcher);
        equSwitcherService.refreshInit();
        addMessage(redirectAttributes, "删除设备成功");
        return "redirect:" + adminPath + "/biz/switcher?officeId="+officeId;
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public String save(EquSwitcher equSwitcher) {
        if(equSwitcher!=null) {
            StringBuilder sb = new StringBuilder();
            for(int i= 0; i<equSwitcher.getTurnWeekArray().length; i++){
                sb.append(equSwitcher.getTurnWeekArray()[i]);
                if(i<equSwitcher.getTurnWeekArray().length-1){
                    sb.append(",");
                }
            }
            equSwitcher.setTurnWeek(sb.toString());
        }
        equSwitcherService.save(equSwitcher);
        equSwitcherService.refreshInit();
        return "success";
    }

    @RequestMapping(value = "sgroupSelectForSysStation", method = RequestMethod.POST)
    public String sgroupSelectForSysStation(HttpServletResponse response) {
        Iterator<StationGroup> it = sglist.iterator();
        while(it.hasNext()){
            StationGroup sg = it.next();
            if (sg.getGrpType().equals("1")){
                it.remove();
            }
        }
            return renderString(response, sglist);
    }


}
